#!/bin/bash

# Start only if at least one xinetd-based service is activated.

test "$(ls -A ${OMD_ROOT}/etc/xinetd.d/)" || exit 5

PIDFILE=${OMD_ROOT}/tmp/run/xinetd.pid

# For SLES we ship our own xinetd. Prefer the one shipped with OMD in case it
# is available
if [ -e "$OMD_ROOT/bin/xinetd" ]; then
    DAEMON=$OMD_ROOT/bin/xinetd
else
    DAEMON=/usr/sbin/xinetd
fi

OPTS="-pidfile ${OMD_ROOT}/tmp/run/xinetd.pid -filelog ${OMD_ROOT}/var/log/xinetd.log -f ${OMD_ROOT}/etc/xinetd.conf"

case "$1" in
    start)
 	echo -n 'Starting xinetd...'
	if [ -e "$PIDFILE" ] ; then
	    PID=$(cat $PIDFILE)
	    if [ -n "$PID" ] && ps $PID > /dev/null 2>&1 ; then
		echo "still running with pid $PID! Aborting!"
		exit 1
	    fi
	    echo "removing stale pid file..."
	    rm -f $PIDFILE
	fi

        # Do not use usual system daemon path. Some start script
        # (e.g. SLES) will think that the system xinetd is already
        # running and do not start it if the OMD xinetd is running.
        mkdir -p $OMD_ROOT/var/tmp
        cp $DAEMON $OMD_ROOT/var/tmp/xinetd
        $OMD_ROOT/var/tmp/xinetd $OPTS
        echo OK
    ;;
    stop)
	echo -n 'Stopping xinetd...'
        PID=$(cat $PIDFILE 2>/dev/null)
        if [ -z "$PID" ] ; then
	    echo "not running."
        elif kill "$PID" ; then
	    echo "OK"
        else
	    echo "Failed"
        fi
        rm -f $OMD_ROOT/var/tmp/xinetd
    ;;
    restart)
	$0 stop
	$0 start
    ;;
    reload)
	echo -n 'Reloading xinetd...'
	if [ -e "$PIDFILE" ] ; then
	    PID=$(cat $PIDFILE)
	    if kill -HUP $PID
	    then
		echo OK
	    else
		echo ERROR
		exit 1
	    fi
	else
	    echo "not running"
	    exit 1
        fi
    ;;
    status)
	echo -n 'Checking status of xinetd...'
	if [ -e "$PIDFILE" ] ; then
	    PID=$(cat $PIDFILE)
	    if [ -n "$PID" ] && ps $PID > /dev/null 2>&1 ; then
		echo "running"
		exit 0
	    fi
	fi
	echo "stopped"
	exit 1
    ;;
    *)
	echo "Usage: $0 {start|stop|restart|reload|status}"
    ;;
esac
